<template>
  <div>
    <public-modal
      width="980px"
      :footer="false"
      :title="title"
      :publicVisible="publicVisible"
      @handleOk="handleOk"
      @cancel="cancel"
    >
      <div slot="content" class="form-wrapper right-content">
        <div class="search-group">
          <div class="input-wrap">
            <div class="label">人员姓名:</div>
            <div class="input-item">
              <a-input allowClear :maxLength="30" clearable v-model="queryParam.memberName" placeholder="请填写" />
            </div>
          </div>
          <div class="input-wrap">
            <div class="label">身份证号:</div>
            <div class="input-item">
              <a-input allowClear :maxLength="30" clearable v-model="queryParam.idCard" placeholder="请填写" />
            </div>
          </div>
          <div class="btn-wrap">
            <a-button class="searchBtn" @click="searchQuery()" type="primary"> 搜索 </a-button>
            <a-button class="resetBtn" @click="searchReset()" type="primary"> 重置 </a-button>
          </div>
        </div>
        <div>
          <a-table
            ref="table"
            rowKey="id"
            class="tableList"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange"
            :scroll="{ x: 1000 }"
          >
            <template slot="action" slot-scope="records">
              <span class="operation deatil-color" @click="handleBind(records)">绑定</span>
            </template>
          </a-table>
        </div>
      </div>
      <div slot="footer"></div>
    </public-modal>
  </div>
</template>

<script>
import { uploadFile } from '@/api/api'
import { checkIDCard } from '@/utils/validate.js'
import publicModal from '@/components/publicModal'
import { putAction, postAction } from '@/api/manage'
import { isMobile } from '@/utils/validate'
import { JeecgListMixinNew } from '@/mixins/JeecgListMixinNew.js'
import { mentalpatientBingding, getCarePersonList } from '../../../../api/api'

export default {
  name: 'addPoliceModal',
  mixins: [JeecgListMixinNew],
  components: { publicModal },
  props: {
    tid: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      title: '新增',
      url: {
        list: '/common/MinorRecordSelect/selectMinorCareTeamPage',
      },
      record: {},
      status: 0,
      publicVisible: false,
      imageUrl: [],
      fileList: [],
      showUploadList: { showPreviewIcon: false },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 80,
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '人员姓名',
          dataIndex: 'memberName',
          ellipsis: true,
          align: 'center',
          customRender: (t) => {
            return (
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{t}</span>
                </template>
                <div class="ant-table-row-cell-ellipsis">{t}</div>
              </a-tooltip>
            )
          },
        },
        {
          title: '角色',
          dataIndex: 'roleTypeName',
          ellipsis: true,
          align: 'center',
          customRender: (t) => {
            return (
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{t}</span>
                </template>
                <div class="ant-table-row-cell-ellipsis">{t}</div>
              </a-tooltip>
            )
          },
        },
        {
          title: '身份证号',
          dataIndex: 'idCard',
          ellipsis: true,
          align: 'center',
          customRender: (t) => {
            return (
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{t}</span>
                </template>
                <div class="ant-table-row-cell-ellipsis">{t}</div>
              </a-tooltip>
            )
          },
        },
        {
          title: '联系电话',
          dataIndex: 'phone',
          ellipsis: true,
          align: 'center',
          customRender: (t) => {
            return (
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{t}</span>
                </template>
                <div class="ant-table-row-cell-ellipsis">{t}</div>
              </a-tooltip>
            )
          },
        },
        {
          title: '备注信息',
          dataIndex: 'remark',
          ellipsis: true,
          align: 'center',
          customRender: (t) => {
            return (
              <a-tooltip placement="top">
                <template slot="title">
                  <span>{t}</span>
                </template>
                <div class="ant-table-row-cell-ellipsis">{t}</div>
              </a-tooltip>
            )
          },
        },
        {
          title: '操作',
          scopedSlots: { customRender: 'action' },
          ellipsis: true,
          align: 'center',
          width: 100,
          fixed: 'right',
        },
      ],
      bindDataList: [],
      extendParams: {},
      disableMixinCreated: true,
    }
  },
  mounted() {},
  methods: {
    show(record, status = 0) {
      this.record = record
      this.publicVisible = true
      //this.extendParams.minorId = this.tid
      // this.searchReset()
      this.searchQuery()
    },
    async searchQuery() {
      try {
        const { result } = await getCarePersonList({
          memberName: this.queryParam.memberName,
          idCard: this.queryParam.idCard,
        })
        this.dataSource = result.records
      } catch (error) {
        console.log(error)
      }
    },
    remove(file) {
      let uid = file.uid
      let index = this.fileList.findIndex((v) => {
        return v.uid == uid
      })

      this.fileList.splice(index, 1)
      this.imageUrl.splice(index, 1)
    },
    async customRequest(info) {
      let file = info.file
      const formData = new FormData()
      formData.append('file', file)
      if (this.status == 0) formData.append('type', 2)
      else if (this.status == 1) formData.append('type', 63)
      else if (this.status == 2) formData.append('type', 64)
      let res = await uploadFile(formData)
      if (res.code == 200) {
        this.imageUrl.push(res.result.halfPath)
      }
    },
    uploadChange({ file, fileList }) {
      file.status = 'done'
      this.fileList = fileList
    },

    handleOk() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        const { add, edit } = apiPaths[this.status]
        const url = this.record.id ? edit : add
        const method = this.record.id ? putAction : postAction
        if (!this.record.id) this.form.policeAgencyId = this.record.policeAgencyId
        try {
          const res = await method(url, this.form)
          this.$message.success(res.message)
          this.$emit('refresh')
          this.cancel()
        } catch (error) {
          // 处理错误
          console.error('请求失败:', error)
          this.$message.error('请求失败，请稍后重试！')
        }
      })
    },

    cancel() {
      this.publicVisible = false
      this.fileList = []
      this.imageUrl = []
      this.form = {}
    },
    handleBind(records) {
      postAction('/qnMinorCareTeam/addMinorRelation', {
        minorId: this.tid,
        minorType: '0',
        careTeamId: records.id,
      }).then((res) => {
        this.$emit('refresh')
        this.publicVisible = false
      })
    },
  },
}
</script>

<style scoped lang="less"></style>
